import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

///
///* 和Future 不同的是，它可以接收多个异步操作的结果，它常用于会多次读取数据的异步任务场景，
///如网络内容下载、文件读写等。StreamBuilder正是用于配合Stream来展示流上事件（数据）变化的UI组件
///*
/// */
class StreamBuilderWidget extends StatefulWidget {
  final String? text;

  StreamBuilderWidget({this.text}) : super();

  @override
  State<StatefulWidget> createState() {
    return _FutureBuilderState(text ?? "");
  }
}

class _FutureBuilderState extends State {
  String? title;

  _FutureBuilderState(String text) {
    title = text;
  }

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: AppBar(
        title: Text(
          title ?? "",
          style: TextStyle(fontSize: 18),
        ),
      ),
      body: Center(
        child: StreamBuilder<int>(
          stream: counter(),
          builder: (BuildContext context, AsyncSnapshot<int> snapshot) {
            if (snapshot.hasError) return Text('Error: ${snapshot.error}');
            switch (snapshot.connectionState) {
              case ConnectionState.none:
                return Text('没有Stream');
              case ConnectionState.waiting:
                return Text('等待数据...');
              case ConnectionState.active:
                return Text('active: ${snapshot.data}');
              case ConnectionState.done:
                return Text('Stream已关闭');
            }
          },
        ),
      ),
    );
  }
}

Stream<int> counter() {
  return Stream.periodic(Duration(seconds: 1), (i) {
    return i;
  });
}
